<?php


namespace Api\Controller;


/** 
 * 关注操作  
 function:
 mine        我的关注 
 who         关注我的
 follow 	 关注 			#done
 unfollow    去关 			#done
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class FollowController extends UserBaseController 
{

	// 错误码
	const ERROR_1 = 1;

	const ACTION_FANS = 0;
	const ACTION_GAME = 1;


	public $uid;
	public $target_id;

	/** 
	 * 加关注
	 * @author Legend. <xcx_legender@qq.com>
	 * @param  $nickname 
	 * @return 
	 */
	
	public function follow()
	{
		$this->check_param();
		$Event = A('Follow', 'Event');
		$res = $Event->follow($this->uid, $this->target_id);
		if (!$res)
		{
			$this->error(self::$API_ERROR['HAS_FOLLOW'], '已经关注');
		}
		$data['follow_me'] = $Event->is_follow($this->target_id, $this->uid);
		$data['follow_me'] = intval($data['follow_me']);

		//add by susy 返回目标的粉丝数
		$data['fans_num'] = intval($Event->count_follows($this->target_id));
		//end
		
		$this->success('关注成功', $data);
	}


	/** 
	 * 取关
	 * @author Legend. <xcx_legender@qq.com>
	 * @param  $nickname
	 * @return 
	 */
	
	public function unfollow()
	{
		$this->check_param();
		$Event = A('Follow', 'Event');
		$res = $Event->unfollow($this->uid, $this->target_id);
		if (!$res)
		{
			$this->error(self::$API_ERROR['HAS_NO_FOLLOW'], '未关注不能取消');
		}
		$data['follow_me'] = $Event->is_follow($this->target_id, $this->uid);
		$data['follow_me'] = intval($data['follow_me']);

		//add by susy 返回目标的粉丝数
		$data['fans_num'] = intval($Event->count_follows($this->target_id));
		//end

		$this->success('取消成功', $data);
	}


	/** 
	 * 我关注的人
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function mine()
	{
		$data = array();
		$feventObj = A('Api/Follow', 'Event');

		//start modify by susy 加入p页数分页
		$total = $feventObj->count_mine(UID);
		if($total){
			
			$p = I('p', 1, 'intval');
			$timestamp = I('timestamp',0, 'intval');
			$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 10), 'intval');
			$where['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);

			$mine = A('Api/Follow', 'Event')->mine(UID, false, $where, $pager, $p);
			$uEvent = A('Api/User','Event');
			foreach ($mine as $key => $value) {
				$user = $uEvent->detail($value);
				$user['is_follow'] = intval(A('Follow', 'Event')->is_follow($value, UID ) );
				$followers[] = $user;
			}

			$data = array('list'=>$followers, 'total'=>$total);
		}
		//end
		$this->success('',$data);
	}


	/** 
	 * 粉丝 : 关注我的人
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function who()
	{
		$data = array();
		$feventObj = A('Api/Follow', 'Event');
		$uEvent = A('Api/User','Event');
		//start modify by susy 加入p页数分页
		$total = $feventObj->count_follows(UID);
		if($total){

			$p = I('p', 1, 'intval');
			$timestamp = I('timestamp',0, 'intval');
			$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 10), 'intval');

			$where['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);
			$mine = $feventObj->follows(UID, false, $where, $pager, $p);
			foreach ($mine as $key => $value) {
				$user = $uEvent->detail($value);
				$user['is_follow'] = intval(A('Follow', 'Event')->is_follow(UID, $value) );
				$fans[] = $user;
			}

			$data = array('list'=>$fans, 'total'=>$total);
		}
		//end

		$this->success('', $data);
	}

	/** 
	 * 参数检查
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	protected function check_param()
	{
		$this->uid = UID;
		$nickname = I('nickname', '');
		if (!$nickname)
		{
			$this->param_error();
		}
		$target_id = A('Api/User', 'Event')->getUser($nickname);

		//start modify by susy 两个判别信息分开
		if (!$target_id)
		{
			$this->error(self::$API_ERROR['USER'], '用户信息有误');
		}

		if($target_id == UID){
			$this->error(self::$API_ERROR['FOLLOW_SELF'], '不能关注自己');
		}
		//end
		
		$this->target_id = intval($target_id);
	}

}
